package com.why.service.impl;

import ch.qos.logback.core.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.dto.UserDTO;
import com.why.pojo.User;
import com.why.service.UserService;
import com.why.mapper.UserMapper;
import com.why.utils.Md5Util;
import com.why.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
* @author 王-自信
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-11-26 16:12:56
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUserName(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        return userMapper.findByUserName(user.getUsername());
    }

    @Override
    public void register(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        String md5password = Md5Util.getMD5String(user.getPassword());
        user.setPassword(md5password);

        //创建时间和修改时间
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.add(user);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(new Date());//设置更新时间
        userMapper.update(user);
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        //存入加密后的密码
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}




